package org.blankhole.model;

import java.io.Serializable;

/**
 * Page entity
 * 
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/08/16
 * @since 1.0
 */
public class Page implements Serializable {
	
	//local variables
	/**
	 * UID
	 */
	private static final long serialVersionUID = 1358041281512828926L;
	/**
	 * the size of the Page
	 */
	private int size;
	/**
	 * the current of the Page
	 */
	private int current = DEFAULT_START_CURRENT;
	/**
	 * the total items
	 */
	private int total;
	/**
	 * the default initial size of the Page
	 */
	public static final int DEFAULT_PAGE_SIZE = 10;
	/**
	 * the default first current of the Page
	 */
	public static final int DEFAULT_START_CURRENT = 0;
	/**
	 * the default max size of the Page
	 */
	public static final int DEFAULT_PAGE_MAX_SIZE = 10000;
	/**
	 * all records
	 */
	public static final Page ALL = new Page(DEFAULT_START_CURRENT, DEFAULT_PAGE_MAX_SIZE);
	/**
	 * default records
	 */
	public static final Page Default = new Page(DEFAULT_START_CURRENT, DEFAULT_PAGE_SIZE);
	/**
	 * no records
	 */
	public static final Page None = new Page(DEFAULT_START_CURRENT, 0);

	//constructor
	/**
	 * Constructs an Page with an initial current and the default size
	 */
	public Page() {
		this(DEFAULT_START_CURRENT, DEFAULT_PAGE_SIZE);
	}

	/**
	 * Constructs an Page with the specified initial current and size
	 * 
	 * @param current the initial current of the Page
	 * @param size the initial size of the Page
	 * @exception IllegalArgumentException if the specified current is less than 0 or size is negative
	 */
	public Page(int current, int size) {
		setCurrent(current);
		setSize(size);
	}

	//setter and getter
	/**
	 * @return the size
	 */
	public int getSize() {
		return this.size;
	}
	/**
	 * @param size the size to set
	 */
	public void setSize(int size) {
		this.size = size;
	}
	
	/**
	 * @return the current
	 */
	public int getCurrent() {
		return current;
	}
	/**
     * set the current of this Page
     *
     * @param current the initial current of the Page
     * @exception IllegalArgumentException if the specified current is less than 1
     */
	public void setCurrent(int current) {
		if (current < DEFAULT_START_CURRENT) {
			throw new IllegalArgumentException("Illegal current: " + current);
		}
		this.current = current;
	}

	/**
	 * @return the total
	 */
	public int getTotal() {
		return this.total;
	}
	/**
     * set the total of this Page
     *
     * @param total the initial total of the Page
     * @exception IllegalArgumentException if the specified initial total is negative
     */
	public void setTotal(int total) {
		if (total < 0) {
			throw new IllegalArgumentException("Illegal total: " + total);
		}
		this.total = total;
		//adjust the current of Page
		adjustPage();
	}

	/**
	 * @return the count of Page
	 */
	public int getCount() {
		return (this.total + this.size - 1) / this.size;
	}

	/**
	 * adjust the current of Page
	 */
	private void adjustPage() {
//		this.current = this.current > getCount() ? getCount() : this.current;
	}

	/**
	 * Returns <tt>true</tt> if this Page has previous page
	 * 
	 * @return
	 */
	public boolean hasPrevious() {
		return this.current > DEFAULT_START_CURRENT;
	}

	/**
	 * Returns <tt>true</tt> if this Page has next page
	 * 
	 * @return
	 */
	public boolean hasNext() {
		return this.current < getCount() - 1;
	}
	
	/**
	 * the start index of Page
	 * 
	 * @return
	 */
	public int getStart(){
		return this.current * this.size;
	}

	//toString
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Page [size=" + size + ", current=" + current + ", total="
				+ total + "]";
	}
	
}